var author_id = 0;
var comment_id = 0;


//设置当前要投票 or 评论的作者
function setId(id,type='vote')
{
    author_id = id;
    
    if (type == 'vote') {

        $.post(checkVoteUrl, {author_id:author_id,_csrf:_csrf}, function(re){
            // console.log(re);return;
            $("#vote-body").text(re['msg']);
            if (re['status'] === 'fail') {
              $("#confirmVote").attr('disabled',true); 
            } 
        },'json');
    } else {

        $.post(checkCommentsUrl, {author_id:author_id,_csrf:_csrf}, function(re){
            // console.log(re);return;
            $("#docomment-body").text(re['msg']);
            if (re['status'] === 'fail') {
                $("#confirmDocomment").attr('disabled',true); 
            } else {
                $("#confirmDocomment").removeAttr('disabled');  
            }
        },'json');

    }

    
}

// 点击评论该作者
function doComments(content) 
{ 
    // console.log(content);
    if (content=='') return false;
    $.post(addcommentUrl,{author_id:author_id, content:content, _csrf:_csrf},function(re){
       if (re['status'] == 'success') {
            location.reload();
       }
    },'json');
}

// 点赞量最高的评论操作
function likecomment(id) 
{
    comment_id = id;
    var likenumber =  $("#comment"+comment_id).html();
    $.ajax({
        type : 'post',
        url : likecommentUrl,
        data : {comment_id:comment_id, _csrf:_csrf},
        async: false,
        dataType: "json",
        success : function(re){
            // console.log(re.status);return;
            if(re.status == 0) {
                // 未点赞          
                $("#comment"+comment_id).html(++likenumber);
                $("#comment"+comment_id).css("color","red");
            } else {
                // 已点赞
                $("#comment"+comment_id).html(--likenumber);
                $("#comment"+comment_id).css("color","black");
            }
        }})
}

// 确认投票
function vote()
{    
    $.post(voteUrl, {authorid:author_id, _csrf:_csrf}, function(re){

        if ( re.status == 0) {
         $("#vote"+author_id +"> span").removeClass('glyphicon-heart-empty');
         $("#vote"+author_id +"> span").addClass('glyphicon-heart');        
         $("#vote"+author_id).attr('disabled',true);
        
        return false;  

        } else {

            alert('失败');

        }

    },'json');
}  

function getComments(authorid)
{    
    $.post(getCommentsUrl,{authorid:authorid, _csrf:_csrf}, function(re){
        // console.log(re);return;
        $("#commentsModalLabel").text("推荐"+re['author']['author_name']+"的评论");

        var comments_list = '';

        $.each(re['comments'],function(k,v){

            comments_list += '<li class="list-group-item">'+
                                v['comment_content'];
            // 已经点赞改变颜色                                    
            if (re['commentdata'].indexOf(v['comment_id']) >= 0 ) {
                comments_list +=  '<span style="color:red" class="glyphicon glyphicon-thumbs-up pull-right"  id="comments'+v['comment_id']+'" onclick=\"likecomments('+v['comment_id']+')\">'+
                                    v['comment_vote']+
                                  '</span>';
            } else {
                comments_list += '<span class="glyphicon glyphicon-thumbs-up pull-right"  id="comments'+v['comment_id']+'" onclick=\"likecomments('+v['comment_id']+')\">'+v['comment_vote']+'</span>';
            }                               
                                
                comments_list+='</li>';
        }) 
        // console.log( $("#comments-body" > "ul"));return;
        $("#comments-body > ul").empty();
        $("#comments-body > ul").append(comments_list);
    },'json');
} 

function likecomments(id)
{
    comment_id = id;
    var likenumbers =  $("#comments"+comment_id).html();     
    // console.log(likenumbers);       
        $.ajax({
            type : 'post',
            url : likecommentUrl,
            data : {comment_id:comment_id, _csrf:_csrf},
            async: false,
            dataType: "json",
            success : function(re){
                console.log(re);
                if(re.status == 0) {
                    // 未点赞  修改modal框的动画        
                    $("#comments"+comment_id).html(++likenumbers);
                    $("#comments"+comment_id).css("color","red");

                    // 未点赞  修改页面框的动画 
                    $("#comment"+comment_id).html(likenumbers);
                    $("#comment"+comment_id).css("color","red");
                } else {
                    // 已点赞 修改modal框的动画    
                    $("#comments"+comment_id).html(--likenumbers);
                    $("#comments"+comment_id).css("color","black");

                    // 已点赞 修改页面框的动画    
                    $("#comment"+comment_id).html(likenumbers);
                    $("#comment"+comment_id).css("color","black");
                }
            }})
}

function deleteAuthor(id)
{
    author_id = id;
    $.post(deleteauthorUrl,{author_id:author_id,_csrf:_csrf},function(re){
        if (re.status == 0) {

        }


    },'json')
    
    
}
